<template >
  <div class="add_con_form tl" v-loading="loading">
    <el-dialog :close-on-click-modal="false" title="支付配置" :visible.sync="visible" :show="show" @close="$emit('update:show', false)" class="size_13" width="800px">
      <el-form :model="data" :rules="dataYZ" ref="data" class="mar_top" label-width="100px">
        <el-form-item label="支付名称" prop="title">
          <el-col :span="10">
            <el-input placeholder v-model="data.title" style="width: 190px"></el-input>
          </el-col>
          <el-col :span="10">
            <el-form-item label="支付标识" prop="link">
              <el-input placeholder :disabled="true" v-model="data.iden" style="width: 190px"></el-input>
            </el-form-item>
          </el-col>
        </el-form-item>

        <template v-if="data.iden == 'offline'">
          <el-form-item label="微信二维码" prop="username">
            <el-col :span="10">
              <el-input v-model="data.username" style="width: 190px"></el-input>
              <compressimgUpload :onSuccess="onSuccess_username" :isCropper="false" :pic="data.username" :isOneImg="true" :files="[]"></compressimgUpload>
            </el-col>
            <el-col :span="10">
              <el-form-item label="支付宝二维码" prop="app_id">
                <el-input v-model="data.app_id" style="width: 190px"></el-input>
                <compressimgUpload :onSuccess="onSuccess_app_id" :isCropper="false" :pic="data.app_id" :isOneImg="true" :files="[]"></compressimgUpload>
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="银行卡信息" prop="app_secret">
            <el-col :span="10">
              <el-input placeholder v-model="data.app_secret" type="textarea" style="width: 280px"></el-input>
            </el-col>
          </el-form-item>
        </template>
        <template v-else>
          <el-form-item label="商户号" prop="username">
            <el-col :span="10">
              <el-input placeholder v-model="data.username" style="width: 190px"></el-input>
            </el-col>
            <el-col :span="10">
              <el-form-item label="密钥" prop="key">
                <el-input placeholder v-model="data.key" style="width: 190px"></el-input>
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="APP_ID" prop="app_id">
            <el-col :span="10">
              <el-input placeholder v-model="data.app_id" style="width: 190px"></el-input>
            </el-col>
            <el-col :span="10">
              <el-form-item label="APP_secret" prop="app_secret">
                <el-input placeholder v-model="data.app_secret" style="width: 190px"></el-input>
              </el-form-item>
            </el-col>
            
          </el-form-item>
          <el-form-item label="报备商户号" prop="reporting">
            <el-col :span="10">
              <el-input placeholder v-model="data.reporting" style="width: 190px"></el-input>
            </el-col>
          </el-form-item>
        </template>

        <el-form-item label="支付图标" prop="piclink">
          <compressimgUpload :onSuccess="onSuccess" :isCropper="false" :pic="data.piclink" :isOneImg="true" :files="fileList"></compressimgUpload>
        </el-form-item>

        <el-form-item label="支付描述" prop="content">
          <el-input placeholder v-model="data.content" style="width: 280px" clearable></el-input>
        </el-form-item>

        <div class="tc mar_bottom_15">
          <el-button :loading="$store.state.isLoading" size="medium" type="primary" @click.prevent="addUpdate('data')">确定</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import compressimgUpload from "@/components/currency/compressimgUpload.vue";
export default {
  components: {
    compressimgUpload,
  },
  data() {
    return {
      options: [
        {
          value: "",
          label: "",
        },
      ],
      value: "",
      data: {
        title: "",
        link: "",
        username: "",
        key: "",
        app_id: "",
        app_secret: "",
        piclink: "",
        content: "",
        reporting: "",
      },
      fileList: [],
      payIds: "",
      loading: false,
      lock: false,
      dataYZ: {
        //title:[
        //    { min: 11, max: 11, message: '手机格式错误', trigger: 'blur'},
        //]
      },
      visible: this.show,
    };
  },
  props: {
    show: Boolean,
    payId: String,
  },
  methods: {
    onSuccess_username(pic) {
      this.data.username = pic;
    },
    onSuccess_app_id(pic) {
      this.data.app_id = pic;
    },
    onSuccess(pic) {
      this.data.piclink = pic;
      this.showImg = true;
    },
    getDetails(id) {
      var data = {};
      data["id"] = id;
      this.$api.getConfig.payInfo(data).then((res) => {
        // console.log(res);
        this.data = res.data.result;
      });
      // console.log(typeof(data)); //object
      // console.log(data); // {id: 1, sign: "ca3ca63b05dafe8fe3ab8c719055b44c"}
    },
    handChange(val) {
      console.log(val);
      this.data.role_id = val;
    },
    addUpdate(formName) {
      this.lock = true;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var params = {};
          params["id"] = this.payIds;
          params["title"] = this.data.title;
          params["link"] = this.data.link;
          params["username"] = this.data.username;
          params["key"] = this.data.key;
          params["app_id"] = this.data.app_id;
          params["reporting"] = this.data.reporting;
          params["app_secret"] = this.data.app_secret;
          params["content"] = this.data.content;
          params["piclink"] = this.data.piclink;

          this.$api.getConfig.paySave(params).then((res) => {
            if (res.data.code == 200 && res.data.result != null) {
              this.$emit("parent", "这里刷新父页面");
              this.$fnc.alertSuccess({ text: "修改成功" }); //成功弹出框
              this.visible = false;
            } else {
              this.$fnc.alertError(res.data.code_desc, res.data.result); //失败弹出框
            }
          });
        }
      });
      this.lock = false;
    },
  },
  watch: {
    show() {
      this.visible = this.show;
    },
    payId(id) {
      this.payIds = id;
      this.getDetails(id);
    },
  },
  mounted() {
    //this.getRoleList();
  },
  created() {},
};
</script>
